﻿using System;
using System.Reflection;

namespace CXW.Core
{
    /// <summary>静态扩展方法</summary>
    public static class Extension
    {
        /// <summary>按属性名克隆一个属性类实例的属性到另一个属性类实例</summary>
        /// <param name="baseObject">源属性类实例</param>
        /// <param name="targetObj">目标属性类实例</param>
        public static void Clone(this object baseObject, object targetObj)
        {
            //对其中的每一个属性进行赋值
            Type oType = baseObject.GetType();
            PropertyInfo tmpP;
            string sPropertyName = "";
            foreach (PropertyInfo oPropertyInfo in oType.GetProperties())
            {
                sPropertyName = oPropertyInfo.Name;

                try
                {
                    tmpP = targetObj.GetType().GetProperty(sPropertyName);

                    if (tmpP != null)
                    {
                        object oValue = oPropertyInfo.GetValue(baseObject, null);
                        //设置值
                        tmpP.SetValue(targetObj, oValue, null);
                    }
                }
                catch
                {
                }
            }
        }
    }
}
